# -*- coding: utf-8 -*-
import aiohttp
from log import getLogger

logger = getLogger(__name__)

service_host = "127.0.0.1"
service_port = "8395"
service_url = "http://{}:{}".format(service_host, service_port)


def urlencode(data):
    l = []
    for k, v in data.items():
        l.append(str(k) + "=" + str(v))
    return "&".join(l)


class Centre(object):
    """
    中间件请求类
    """

    def __init__(self, loop):
        self.loop = loop

    def add_alarm_record(self, sn, almtype):
        params = {"sn": sn, "almtype": almtype}
        self.loop.run_until_complete(
            self.http_get(service_url + "/middleware/alarm/type",
                          params=params))

    async def http_get(self, url, params):
        try:
            async with aiohttp.request('GET', url, params=params) as resp:
                json = await resp.json()
                logger.info(json)
        except Exception as e:
            logger.error(e)
